
/*----------  Set global source path  ----------*/

global source "[path to folder with downloaded data]"


/*----------  Equity yields  ----------*/

* downloaded from Giglio, Kelly and Kozak's webpage
import delimited "$source/EqTS_synthetic_strip_yields-paper-19NOV2020/EY_mkt.csv", clear

rename date date_string
gen datem = monthly(date_string,"YM")
format datem %tm
order datem
drop date_string

forval h = 1/100 {
	label var ey`h' "Log equity yield `h'y"
}

keep datem ey1-ey15 

tempfile equity_yields
save `equity_yields', replace


/*----------  Bond yields  ----------*/

* downloaded from https://www.federalreserve.gov/data/nominal-yield-curve.htm
import delimited "$source/feds200628.csv", bindquote(nobind) varnames(10) rowrange(10) numericcols(2(1)100) clear
rename date date_string
gen dated = date(date_string,"YMD")
format dated %td
gen datem = mofd(dated)
format datem %tm
order dated datem
drop date_string
foreach var of varlist sven* {
	replace `var' = `var'/100 // from percent
}

keep dated datem sveny??
sort dated
collapse (lastnm) sveny??, by(datem)

* convert to log bond yields
rename sveny??  by??
forval h = 1/9 {
	rename by0`h' by`h'
}
forval h=1/30 {
	replace by`h' = log(by`h' + 1)
	label var by`h' "Log bond yield `h'y"
}

keep datem by1-by15 

tempfile bond_yields
save `bond_yields', replace


/*----------  Dividends  ----------*/

* monthly series dowloaded from WRDS CRSP Index File on the S&P 500 (annual update)
import delimited "$source/sp500.csv", clear
gen dated = date(caldt,"YMD")
format dated %td
gen datem = mofd(dated)
format datem %tm
drop caldt dated
order datem

tsset datem
gen dividends = (vwretd - vwretx) * l.totval

keep datem dividends

tempfile dividends
save `dividends', replace


/*----------  Recessions  ----------*/

* downloaded from FRED
import delimited "$source/recession.csv", clear
rename datem date_string
gen datem = monthly(date_string,"YM")
format datem %tm
order datem
drop date_string

tempfile recession
save `recession', replace


/*----------  Combine data  ----------*/

use `bond_yields', clear
merge 1:1 datem using `dividends', assert(1 3) keep(3) nogen
merge 1:1 datem using `equity_yields', assert(1 3) nogen
merge 1:1 datem using `recession', assert(2 3) nogen

* Forward yields
forval h = 1/15 {
	gen fy`h' = ey`h' - by`h'
}

* Sum of dividends paid in the past year
rangestat (sum) dividends_12m = dividends (count) count=dividends, int(date -11 0)
replace dividends_12m = . if count<12
drop count

* Equity yields start 1974m9 and end 2020m8
keep if inrange(datem,tm(1974m9),tm(2020m8))

tsset date
sort date
forval lag = 0/11 {
	gen recession_l`lag' = l`lag'.recession
}
egen recession_in_past_year = rowtotal(recession_l*)
drop recession_l*

rename datem date
order date recession recession_in_past_year dividends dividends_12m

export delimited using "yields_recession_2023R.csv", replace
